package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.*;
import com.cqut.hfh.ordering.service.IModuleService;
import com.cqut.hfh.ordering.service.IRoleService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/22-22:17
 */
@Service
public class RoleServiceImpl implements IRoleService {


    @Resource
    private SearchDao searchDao;

    @Resource
    private Dao dao;

    @Override
    public List<EhyRole> getRolesById(String man_id) {
        String condition = " ehy_manager_role.man_id = '" + man_id + "'";
        String joinEntity = " left join ehy_role on ehy_role.role_id = ehy_manager_role.role_id";

        List<Map<String, Object>> role_list = searchDao.searchForeign(new String[]{"ehy_role.role_id",
                "ehy_role.role_name",
                "ehy_role.role_code",
                "ehy_role.optime"},
                Entity.getTableName(EhyManagerRole.class),
                joinEntity,
                null,
                null,
                condition);

        if(role_list == null){
            return null;
        }

        List<EhyRole> ehyRole_list = new ArrayList<>();
        for( Map<String, Object> temp : role_list){
            EhyRole ehyRole_temp = new EhyRole();
            ehyRole_temp.setProperties(temp);
            ehyRole_list.add(ehyRole_temp);
        }
        return ehyRole_list;
    }

    @Override
    public List<Map<String, Object>> getRoles(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> roles_list
                = dao.findByCondition(new String[]{"role_id",
                "role_name",
                "role_code",
                "role_desc",
                "optime",
                "oper",
                "isva"},
                con,
                EhyRole.class);

        return roles_list;
    }

    @Override
    public int delById(String role_id) {
        int a = dao.deleteByID(role_id, EhyRole.class);
        int b = delModulesById(role_id);
        int c = dao.deleteByCondition(" role_id='" + role_id + "'", EhyManagerRole.class);
        return a + b + c;
    }

    @Override
    public int delModulesById(String role_id) {
        return dao.deleteByCondition(" role_id = '" + role_id + "'", EhyRoleModule.class);
    }

    @Override
    public Map<String, Object> findById(String role_id) {
        return dao.getByID(role_id, EhyRole.class).toMap();
    }

    @Override
    public int saveRole(EhyRole role, String module_ids) {
        int save = dao.save(role);

        String[] mod_strs = module_ids.split(",");

        for( String mod_str : mod_strs ){
            EhyRoleModule rm = new EhyRoleModule();
            rm.setRole_id(role.getRole_id());
            rm.setRm_id(EntityIdutils.getId());
            rm.setMod_id(mod_str);
            dao.save(rm);
        }

        return save;
    }

    @Override
    public int getCount() {
       return searchDao.sqlCount(Entity.getTableName(EhyRole.class));
    }

    @Override
    public int updateRole(EhyRole role, String module_ids) {

        String[] mod_strs = module_ids.split(",");

        int a = dao.updatePropByID(role, role.getRole_id());

        dao.deleteByCondition(" role_id='" + role.getRole_id() + "'", EhyRoleModule.class);


        for( String mod_str : mod_strs ){
                EhyRoleModule rm = new EhyRoleModule();
                rm.setRm_id(EntityIdutils.getId());
                rm.setRole_id(role.getRole_id());
                rm.setMod_id(mod_str);
                dao.save(rm);
            }
        return 1;
    }

    @Override
    public List<Map<String, Object>> getRolesByName(String s, String page, String role_name) {
        String con = " role_name like '%" + role_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(s) + "," + s ;
        List<Map<String, Object>> roles_list
                = dao.findByCondition(new String[]{"role_id",
                        "role_name",
                        "role_code",
                        "role_desc",
                        "optime",
                        "oper",
                        "isva"},
                con,
                EhyRole.class);

        return roles_list;
    }

    @Override
    public List<EhyRole> getRole() {
        return dao.getByCondition("",EhyRole.class);
    }

}

